package nxu.it.dao.Impl;

import nxu.it.dao.JdbiFactory;
import nxu.it.dao.TodoDao;
import nxu.it.dao.TodoService;
import nxu.it.model.Todo;
import org.jdbi.v3.core.Jdbi;

import java.util.List;
import java.util.Optional;

public class JdbiTodoServiceImpl implements TodoService {
    private static final Jdbi JDBI = JdbiFactory.getJdbi();

    @Override
    public List<Todo> list() {
//        String sql = "SELECT id, content, complete, create_at, update_at FROM todo";
//        List<Todo> todoList = JDBI.withHandle(handle->
//            handle.createQuery(sql)
//                    .mapToBean(Todo.class)
//                    .list()
//        );
//        return todoList;
        return JDBI.withExtension(TodoDao.class, dao->dao.list());
    }

    @Override
    public List<Todo> listByUser(String username) {
        return JDBI.withExtension(TodoDao.class, dao->dao.listByUser(username));
    }
    @Override
    public int listByUserCount(String username) {
        return JDBI.withExtension(TodoDao.class, dao->dao.listByUserCount(username));
    }
    @Override
    public Boolean save(Todo todo) {
//        if(todo == null){
//            return false;
//        }
//        String sql = "Insert INTO todo (content, complete, create_at, update_at,username) VALUES (:content,:complete,:create_at,:update_at,:username)";
//        JDBI.withHandle(handle->
//                handle.createUpdate(sql)
////                        .bind("complete",todo.getComplete())
////                        .bind("content",todo.getContent())
////                        .bind("create_at",todo.getCreateAt())
////                        .bind("update_at",todo.getUpdateAt())
////                        .bind("username",todo.getUsername())
//                        .bindBean(todo)
//                        .execute()
//        );
//        return true;
        return JDBI.withExtension(TodoDao.class, dao->{
           try{
               dao.save(todo);
           }catch (Exception e){
               return false;
           }
           return true;
        });
    }

    @Override
    public Boolean updateByUser(Todo todo, String username) {
        if(todo == null || (todo.getUsername() != null && !todo.getUsername().equals(username))){
            return false;
        }
        return JDBI.withExtension(TodoDao.class, dao->{
            try{
                dao.updateByUser(todo,username);
            }catch (Exception e){
                return false;
            }
            return true;
        });
    }

    @Override
    public Optional<Todo> get(int id) {
        return JDBI.withExtension(TodoDao.class, dao->dao.get(id));
    }

    @Override
    public Optional<Todo> getByUser(int id, String username) {
        return JDBI.withExtension(TodoDao.class, dao->dao.getByUser(id,username));
    }

    @Override
    public Boolean deleteByUser(int id, String username) {
        return JDBI.withExtension(TodoDao.class, dao->{
            try{
                dao.deleteByUser(id,username);
            }catch (Exception e){
                return false;
            }
            return true;
        });
    }

    @Override
    public void checkByUser(int id, String username) {
        JDBI.useExtension(TodoDao.class,dao->dao.checkByUser(id,username));
    }
}
